1
|
|
|
import {readFileSync, writeFileSync} from 'fs' |
2
|
|
|
import {Config} from '../models/config' |
3
|
|
|
import Nginx from '../services/nginx' |
4
|
|
|
import {error, success, url} from '../utils/console' |
5
|
|
|
import {getConfig, jaleSitesPath} from '../utils/jale' |
6
|
|
|
import {serverNamesRegex} from '../utils/regex' |
7
|
|
|
|
8
|
|
|
class SubdomainController { |
9
|
|
|
|
10
|
|
|
config: Config |
11
|
|
|
project: string |
12
|
|
|
hostname: string |
13
|
|
|
|
14
|
|
|
constructor() { |
15
|
|
|
this.config = getConfig() |
16
|
|
|
this.project = process.cwd().substring(process.cwd().lastIndexOf('/') + 1) |
17
|
|
|
|
18
|
|
|
const vhostConfig = readFileSync(`${jaleSitesPath}/${this.project}.conf`, 'utf-8') |
19
|
|
|
const serverNames = serverNamesRegex.exec(vhostConfig) ?? [] |
20
|
|
|
|
21
|
|
|
serverNamesRegex.lastIndex = 0 |
22
|
|
|
|
23
|
|
|
this.hostname = `${this.project}.${this.config.tld}` |
24
|
|
|
|
25
|
|
|
if (serverNames[0].split(' ').length > 1) { |
26
|
|
|
this.hostname = serverNames[0].split(' ')[1] |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
execute = async (option: string, subdomain: string): Promise<void> => { |
31
|
|
|
if (option !== 'add' && option !== 'del') { |
32
|
|
|
error('Invalid option. Please use \'add\' or \'del\', followed by the subdomain.') |
33
|
|
|
return |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
let restartNginx = false |
37
|
|
|
|
38
|
|
|
if (option === 'add') { |
39
|
|
|
restartNginx = this.addSubdomain(subdomain) |
40
|
|
|
} |
41
|
|
|
else if (option === 'del') { |
42
|
|
|
restartNginx = this.deleteSubdomain(subdomain) |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if (restartNginx) |
46
|
|
|
await (new Nginx()).reload() |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Check if the subdomain already exists in the vhost's Nginx configuration. |
51
|
|
|
* |
52
|
|
|
* @param subdomain |
53
|
|
|
*/ |
54
|
|
|
subdomainExists = (subdomain: string): boolean => { |
55
|
|
|
try { |
56
|
|
|
const vhostConfig = readFileSync(`${jaleSitesPath}/${this.project}.conf`, 'utf-8') |
57
|
|
|
return vhostConfig.includes(`${subdomain}.${this.hostname}`) |
58
|
|
|
} catch (e) { |
59
|
|
|
return false |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Add a new subdomain to the vhost's Nginx configuration. |
65
|
|
|
* |
66
|
|
|
* @param subdomain |
67
|
|
|
*/ |
68
|
|
|
addSubdomain = (subdomain: string): boolean => { |
69
|
|
|
if (this.subdomainExists(subdomain)) { |
70
|
|
|
error(`Subdomain ${subdomain}.${this.hostname} already exists.`) |
71
|
|
|
return false |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
let vhostConfig = readFileSync(`${jaleSitesPath}/${this.project}.conf`, 'utf-8') |
75
|
|
|
const rawServerNames = serverNamesRegex.exec(vhostConfig) |
76
|
|
|
|
77
|
|
|
if (!rawServerNames) { |
78
|
|
|
return false // TODO: Catch this issue |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
const serverNames = rawServerNames[0].split(' ') |
82
|
|
|
serverNames.push(`${subdomain}.${this.hostname}`) |
83
|
|
|
|
84
|
|
|
// Replace the old server names with the server names including the new subdomain. |
85
|
|
|
vhostConfig = vhostConfig.replace(serverNamesRegex, serverNames.join(' ')) |
86
|
|
|
|
87
|
|
|
writeFileSync(`${jaleSitesPath}/${this.project}.conf`, vhostConfig) |
88
|
|
|
|
89
|
|
|
success(`Added subdomain ${url(`${subdomain}.${this.hostname}`)}.`) |
90
|
|
|
|
91
|
|
|
return true |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Delete a subdomain from the vhost's Nginx configuration. |
96
|
|
|
* |
97
|
|
|
* @param subdomain |
98
|
|
|
*/ |
99
|
|
|
deleteSubdomain = (subdomain: string): boolean => { |
100
|
|
|
if (!this.subdomainExists(subdomain)) { |
101
|
|
|
error(`Subdomain ${url(`${subdomain}.${this.hostname}`)} does not exist.`) |
102
|
|
|
return false |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
let vhostConfig = readFileSync(`${jaleSitesPath}/${this.project}.conf`, 'utf-8') |
106
|
|
|
|
107
|
|
|
const rawServerNames = serverNamesRegex.exec(vhostConfig) |
108
|
|
|
|
109
|
|
|
if (!rawServerNames) { |
110
|
|
|
return false // TODO: Catch this issue |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
const serverNames = rawServerNames[0].split(' ') |
114
|
|
|
serverNames.splice(serverNames.indexOf(`${subdomain}.${this.hostname}`), 1) |
115
|
|
|
|
116
|
|
|
// Replace the old server names with the new list without the removed subdomain. |
117
|
|
|
vhostConfig = vhostConfig.replace(serverNamesRegex, serverNames.join(' ')) |
118
|
|
|
|
119
|
|
|
writeFileSync(`${jaleSitesPath}/${this.project}.conf`, vhostConfig) |
120
|
|
|
|
121
|
|
|
success(`Removed subdomain ${subdomain}.${this.hostname}.`) |
122
|
|
|
|
123
|
|
|
return true |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
export default SubdomainController |